package com.centaline.androidsalesblog.activities;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alexvasilkov.gestures.commons.DepthPageTransformer;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.widgets.IndicatorView;

/**
 * 引导页
 * Created by guilin on 16/5/19.
 */
public class GuideActivity extends BaseActivity {

    private final int[] IMGS = new int[]{R.drawable.ic_lead_1,
            R.drawable.ic_lead_2, R.drawable.ic_lead_3, R.drawable.ic_lead_4};

    private ViewPager viewPager;
    private AppCompatTextView atv_enter;
    private IndicatorView indicatorView;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_guide;
    }

    @Override
    protected void findViews() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        atv_enter = (AppCompatTextView) findViewById(R.id.atv_enter);
        indicatorView = (IndicatorView) findViewById(R.id.indicatorView);
    }

    @Override
    protected void initViews() {
        viewPager.setPageTransformer(true, new DepthPageTransformer());
        viewPager.setAdapter(new GuideAdapter());
        indicatorView.setViewPager(viewPager);
        indicatorView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                atv_enter.setVisibility(position + 1 == IMGS.length ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        atv_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(GuideActivity.this, MainActivity.class));
                finish();
            }
        });
    }

    class GuideAdapter extends PagerAdapter {

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getCount() {
            return IMGS.length;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            ImageView image = new ImageView(getApplicationContext());
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            image.setImageResource(IMGS[position]);
            container.addView(image, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            return image;
        }
    }

}
